<?php
namespace Home\Controller;

/**
 * 美住微预定设置页面
 * @modify trainyao 2016-04-27 创建
 */

class BookSettingController extends ParentController
{
    private $hasRight = true;

    protected function _initialize() {
        parent::_initialize();
        $this->validateUtil = new \Org\Util\Validate();

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            $this->hasRight = false;
        }
        $hotelId = $param['hotel'];
        // 校验权限,目前只有超级管理员能访问
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $result = $adminAuthorityLogic->check($hotelId);
        if ($result == false) {
            $this->hasRight = false;
        }
        // 检查客栈是否已过期
        $hotelLogic = A('Common/Hotel', 'Logic');
        $result = $hotelLogic->checkHotelActive($hotelId);
        if ($result == false) {
            $this->hasRight = false;
        }
        $result = $hotelLogic->checkHotelStatus($hotelId);
        if ($result == false) {
            $this->hasRight = false;
        }
    }

    /**
     * 开通微预定
     * @modify trainyao 2016-04-28    创建
     * @modify wenhui 2017-05-04 增加国际手机检验
     */
    public function enable() {
        if (!$this->hasRight) {
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'address' => 'notnull,notempty,nameLength50',
            'contact' => 'notnull,notempty,nameLength20',
            'mobile' => 'notnull,notempty',
            'description' => 'notnull,notempty,nameLength255',
            'orderTimeout' => 'notnull,notempty,orderTimeout',
            'longtitude' => 'notempty,decimal9_6',
            'latitude' => 'notempty,decimal9_6',
            'telephone' => 'notempty,nameLength15',
            'wechat' => 'notempty,nameLength20',
            'hotline' => 'notempty,nameLength15',
        );

        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        $areaCode = I('post.areaCode', '86', 'string');
        $mobile = $param['mobile'];
        $this->ajaxValidateReturn($this->validateUtil, $mobile, 'globalMobile',
                    array('globalMobile' => array('function', 'checkGlobalMobile,' . $areaCode, '403,130,error'),
                ));

        // 已开通报错
        $hotelId = $param['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if ($isEnable) {
            returnJson(403, 410);
            return;
        }
        
        //获取客栈名称
        $hotelEntityModel = D('HotelEntity');
        $hotelEntityInfo = $hotelEntityModel->getInfo($hotelId);
        if(empty($hotelEntityInfo)){
            return ;
        }
        $name = $hotelEntityInfo['name'];
        $enddate = $hotelEntityInfo['enddate'];

        $contact = $param['contact'];
        $address = $param['address'];
        $description = $param['description'];
        $orderTimeout = $param['orderTimeout'];
        $longtitude = $param['longtitude'];
        $latitude = $param['latitude'];
        $telephone = $param['telephone'];
        $wechat = $param['wechat'];
        $hotline = $param['hotline'];

        $enableResult = $hotelBookLogic->enableBook($hotelId, $name, $contact, $mobile, $address, $description, $orderTimeout, $longtitude, $latitude, $telephone, $wechat, $hotline, $enddate, $areaCode);
        if (empty($enableResult)) {
            return;
        }
        returnJson(200, 200);
    }

    /**
     * 获取微预定信息
     * @modify trainyao 2016-04-28    创建
     */
    public function info() {
        if (!$this->hasRight) {
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
        );

        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        $hotelId = $param['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        $hotelDetail = $hotelBookLogic->getInfo($hotelId);
        // 处理数据
        unset($hotelDetail['enable']);
        $hotelDetail['orderTimeout'] = $hotelDetail['ordertimeout'];
        unset($hotelDetail['ordertimeout']);
        unset($hotelDetail['specialimageurl']);
        unset($hotelDetail['coverimageurl']);
        unset($hotelDetail['special']);
        unset($hotelDetail['specialTag']);
        unset($hotelDetail['gallery']);    

        // 获取认证信息
        $hotelAuthModel = D('HotelAuth');
        $hotelAuthInfo = $hotelAuthModel->getInfo($hotelId); 
        if(empty($hotelAuthInfo)){
            $hotelDetail['authInfo'] = array(
                'hotelname' => '',
                'contact' => '',
                'telephone' => '',
                'address' => '',
                'status' => 0,
            );
        }
        else {      
            $status = $hotelAuthInfo['status'];
            if($status=='2' && (empty($hotelDetail['certified'])||time()>strtotime($hotelDetail['certifiedenddate']))){
                $status = '4';
            }
            $hotelDetail['authInfo'] = array(
            	'hotelname' => $hotelAuthInfo['hotelname'],
            	'contact' => $hotelAuthInfo['contact'],
            	'telephone' => $hotelAuthInfo['telephone'],
            	'address' => $hotelAuthInfo['address'],
            	'status' => $status,
            );
        }
        unset($hotelDetail['certified']);
        unset($hotelDetail['certifiedenddate']);

        $hotelDetail['usable'] = !empty($hotelDetail['opened']);
        $hotelDetail['overdue'] = time()>strtotime($hotelDetail['enddate']);
        unset($hotelDetail['opened']);
        unset($hotelDetail['enddate']);
        
        returnJson(200, 200, $hotelDetail);
    }

    /**
     * 获取封面图片信息
     * @modify trainyao 2016-04-28    创建
     */
    public function cover() {
        if (!$this->hasRight) {
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
        );

        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        $hotelId = $param['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        $hotelDetail = $hotelBookLogic->getInfo($hotelId);
        // 处理数据
        unset($hotelDetail['name']);
        unset($hotelDetail['address']);
        unset($hotelDetail['contact']);
        unset($hotelDetail['description']);
        unset($hotelDetail['longtitude']);
        unset($hotelDetail['latitude']);
        unset($hotelDetail['facility']);
        unset($hotelDetail['mobile']);
        unset($hotelDetail['telephone']);
        unset($hotelDetail['wechat']);
        unset($hotelDetail['enable']);
        unset($hotelDetail['ordertimeout']);
        unset($hotelDetail['specialimageurl']);
        unset($hotelDetail['special']);
        unset($hotelDetail['specialTag']);
        unset($hotelDetail['gallery']);
        $hotelDetail['coverImageUrl'] = $hotelDetail['coverimageurl'];
        unset($hotelDetail['coverimageurl']);

        returnJson(200, 200, $hotelDetail);
    }

    /**
     * 获取个性推荐信息
     * @modify trainyao 2016-04-28    创建
     * @modify wenhui 2016-10-28    修改数据结构
     */
    public function special() {
        if (!$this->hasRight) {
            return;
        }
        
        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        $hotelId = $param['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        $hotelDetail = $hotelBookLogic->getInfo($hotelId);
        // 处理数据
        unset($hotelDetail['special']);
        unset($hotelDetail['name']);
        unset($hotelDetail['address']);
        unset($hotelDetail['contact']);
        unset($hotelDetail['description']);
        unset($hotelDetail['longtitude']);
        unset($hotelDetail['latitude']);
        unset($hotelDetail['facility']);
        unset($hotelDetail['mobile']);
        unset($hotelDetail['telephone']);
        unset($hotelDetail['wechat']);
        unset($hotelDetail['enable']);
        unset($hotelDetail['ordertimeout']);
        unset($hotelDetail['gallery']);
        unset($hotelDetail['coverimageurl']);
        unset($hotelDetail['specialimageurl']);
        foreach ($hotelDetail['specialTag'] as &$hotelSpecialTagItem) {
            unset($hotelSpecialTagItem['id']);
        }
        unset($hotelSpecialTagItem);

        // 获取hotelspecial表中的数据 组装新的数据格式
        $hotelSpecialDetail = $hotelBookLogic->getSpecialInfo($hotelId);
        $hotelDetail['specialInfo'] = array();
        foreach ($hotelSpecialDetail as &$hotelSpecialItems) {
            // 处理数据
            unset($hotelSpecialItems['hotelentity_id']);
            $hotelSpecialItem = array(
                'specialId' => $hotelSpecialItems['id'],
                'special' => $hotelSpecialItems['special'],
                'specialImageUrl' => $hotelSpecialItems['specialimageurl'],
                'isactive' => $hotelSpecialItems['isactive'],
                'index' => $hotelSpecialItems['sortindex'],
            );
            array_push($hotelDetail['specialInfo'], $hotelSpecialItem);
        }
        unset($hotelSpecialItems);

        // 按‘index’ 升序
        $hotelDetail['specialInfo'] = my_sort($hotelDetail['specialInfo'], 'index', SORT_ASC, SORT_NUMERIC);
        
        returnJson(200, 200, $hotelDetail);
    }

    /**
     * 获取个性推荐信息
     * @modify trainyao 2016-04-28    创建
     */
    public function gallery() {
        if (!$this->hasRight) {
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
        );

        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        $hotelId = $param['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        $hotelDetail = $hotelBookLogic->getInfo($hotelId);
        // 处理数据
        unset($hotelDetail['name']);
        unset($hotelDetail['address']);
        unset($hotelDetail['contact']);
        unset($hotelDetail['description']);
        unset($hotelDetail['longtitude']);
        unset($hotelDetail['latitude']);
        unset($hotelDetail['facility']);
        unset($hotelDetail['mobile']);
        unset($hotelDetail['telephone']);
        unset($hotelDetail['wechat']);
        unset($hotelDetail['enable']);
        unset($hotelDetail['ordertimeout']);
        unset($hotelDetail['coverimageurl']);
        unset($hotelDetail['specialimageurl']);
        unset($hotelDetail['special']);
        unset($hotelDetail['specialTag']);
        $hotelDetail['gallery'] = array_column($hotelDetail['gallery'], 'name');

        returnJson(200, 200, $hotelDetail);
    }

    /**
     * 保存客栈微预定信息
     * @modify trainyao 2016-04-28    创建
     * @modify wenhui 2017-05-04 增加国际手机检验
     */
    public function save() {
        if (!$this->hasRight) {
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'description' => 'notempty,nameLength255',
            'address' => 'notempty,nameLength50',
            'longtitude' => 'notempty,decimal9_6',
            'latitude' => 'notempty,decimal9_6',
            'contact' => 'notempty,nameLength20',
            //'mobile' => 'notempty,mobile',
            'telephone' => 'notempty,nameLength15',
            'wechat' => 'notempty,nameLength20',
            'hotline' => 'notempty,nameLength15',
            'orderTimeout' => 'notempty,orderTimeout',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validatePass) {
            return;
        }
        if (isset($param['facility'])) {
            $facility = $param['facility'];
            if ($facility != '') {
                $validatePass = $this->ajaxValidateReturn($this->validateUtil, $facility, 'concatId');
                if (!$validatePass) {
                    return;
                }
                $facility = explode(',', $facility);
            } else {
                $facility = array();
            }
        } else {
            $facility = null;
        }

        $hotelId = $param['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }
        
        $name = null;
        $description = isset($param['description']) ? $param['description'] : null;
        $address = isset($param['address']) ? $param['address'] : null;
        $longtitude = isset($param['longtitude']) ? $param['longtitude'] : null;
        $latitude = isset($param['latitude']) ? $param['latitude'] : null;
        $contact = isset($param['contact']) ? $param['contact'] : null;
        $mobile = isset($param['mobile']) ? $param['mobile'] : null;
        $telephone = isset($param['telephone']) ? $param['telephone'] : '';
        $wechat = isset($param['wechat']) ? $param['wechat'] : '';
        $hotline = isset($param['hotline']) ? $param['hotline'] : '';
        $orderTimeout = isset($param['orderTimeout']) ? $param['orderTimeout'] : null;
        $areaCode = I('post.areaCode', '86', 'string');
        // 检查手机区号与号码
        $this->ajaxValidateReturn($this->validateUtil, $mobile, 'globalMobile',
                    array('globalMobile' => array('function', 'checkGlobalMobile,' . $areaCode, '403,130,error'),
                ));
        $saveResult = $hotelBookLogic->saveDetail($hotelId, $facility, $name, $description, $contact, $mobile, $telephone, $wechat, $address, $longtitude, $latitude, $orderTimeout, $hotline, $param['logoImageUrl'], $areaCode);
        if (empty($saveResult)) {
            return;
        }

        $hotelAuthModel = D('HotelAuth');
        $hotelAuthInfo = $hotelAuthModel->getInfo($hotelId);
        if(!empty($hotelAuthInfo)){
            if($hotelAuthInfo['contact']!=$param['contact'] || $hotelAuthInfo['telephone']!=$param['mobile'] || $hotelAuthInfo['address']!=$param['address']){
                //丢掉认证标志
                $where = array(
                    'hotelentity_id' => $hotelId
                );
                $data = array(
                    'certified' => 0,
                );
                $hotelDetailModel = D('HotelDetail');
                $hotelDetailModel->where($where)->save($data);
                //变为未申请
                $data = array(
                    'status' => 0,
                );
                $hotelAuthModel->where($where)->save($data);
            }
        }
        
        returnJson(200, 200);
    }

    /**
     * 获取地址经纬度信息
     * @modify trainyao 2016-04-28    创建
     */
    public function geoInfo() {
        if (!$this->hasRight) {
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'address' => 'notnull,notempty,nameLength50',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validatePass) {
            return;
        }

        // 调用百度Api,获取经纬度
        $address = $param['address'];
        $opts = array(
            'http' => array(
                'method' => "GET",
                'timeout' => 10,
            ));
        $context = stream_context_create($opts);
        $url = "http://api.map.baidu.com/geocoder?address=" . urlencode($address) . "&output=json&key=37492c0ee6f924cb5e934fa08c6b1676&";
        $baiduData = file_get_contents($url, false, $context);
        $baiduData = iconv("GB2312", "UTF-8//IGNORE", $baiduData);
        $baiduData = json_decode($baiduData, true);

        if (isset($baiduData['result']) && isset($baiduData['result']['location']) && isset($baiduData['result']['location']['lng']) && isset($baiduData['result']['location']['lat'])) {
            returnJson(200, 200, array(
                'longtitude' => $baiduData['result']['location']['lng'],
                'latitude' => $baiduData['result']['location']['lat'],
            ));
            return;
        } else {
            returnJson(403, 600);
            return;
        }
    }

    /**
     * 保存客栈封面图片
     * @modify trainyao 2016-04-28    创建
     */
    public function saveCoverImg() {
        if (!$this->hasRight) {
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'url' => 'notnull,notempty,nameLength255',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validatePass) {
            return;
        }

        $hotelId = $param['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        $url = $param['url'];
        $hotelDetailModel = D('HotelDetail');
        $saveResult = $hotelDetailModel->saveInfo($hotelId, null, null, null, null, $url, null, null, null, null, null, null, null, null);
        if (empty($saveResult)) {
            returnJson(500, 506);
            return;
        }
        returnJson(200, 200);
    }

    /**
     * 保存客栈封面图片
     * @modify trainyao 2016-04-28    创建
     */
    public function saveGallery() {
        if (!$this->hasRight) {
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'url' => 'notnull',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validatePass) {
            return;
        }
        $url = $param['url'];
        if ($url == '') {
            $url = array();
        } else {
            $url = explode(',', $param['url']);
            foreach ($url as $urlItem) {
                $validatePass = $this->ajaxValidateReturn($this->validateUtil, $urlItem, 'nameLength255');
                if (!$validatePass) {
                    return;
                }
            }
            unset($urlItem);
        }
        // 相册只能保存4张
        if (count($url) > 4) {
            returnJson(403, 202);
            return;
        }

        $hotelId = $param['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        $saveResult = $hotelBookLogic->saveGallery($hotelId, $url);
        if (empty($saveResult)) {
            return;
        }
        returnJson(200, 200);
    }

    /**
     * 保存个性推荐文字内容
     * @modify trainyao 2016-04-28    创建
     */
    public function saveSpecialText() {
        if (!$this->hasRight) {
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'specialId' => 'notnull,notempty,id',
            'special' => 'notnull,notempty,nameLength100',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validatePass) {
            return;
        }
        
        $hotelId = $param['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        $special = $param['special'];
        $specialId = $param['specialId'];
        $saveResult = $hotelBookLogic->saveSpecial($hotelId, $specialId, null, $special);
        if (!$saveResult) {
            return;
        }
        
        $data = array(
            'special' => $special,
            'specialId' => $specialId,
        );
        returnJson(200, 200, $data);
    }

    /**
     * 保存个性推荐标签
     * @modify trainyao 2016-04-28    创建
     */
    public function saveSpecialTag() {
        if (!$this->hasRight) {
            return;
        }
        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'tag' => 'notnull',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validatePass) {
            return;
        }
        $tag = $param['tag'];
        if ($tag == '') {
            $tag = array();
        } else {
            $tag = explode(',', $tag);
            foreach ($tag as $tagItem) {
                $validatePass = $this->ajaxValidateReturn($this->validateUtil, $tagItem, 'nameLength15');
                if (!$validatePass) {
                    return;
                }
            }
            unset($tagItem);
        }
        // 标签最多3个
        if (count($tag) > 3) {
            returnJson(403, 202);
            return;
        }

        $hotelId = $param['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        $saveResult = $hotelBookLogic->saveSpecialInfo($hotelId, null, null, $tag);
        if (empty($saveResult)) {
            return;
        }
        returnJson(200, 200);
    }

    /**
     * 保存个性推荐图片
     * @modify trainyao 2016-04-28    创建
     */
    public function saveSpecialImage() {
        if (!$this->hasRight) {
            return;
        }
    
        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'specialId' => 'notnull,notempty,id',
            'specialImageUrl' => 'notnull,notempty,nameLength255',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validatePass) {
            return;
        }
        
        $hotelId = $param['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        $specialId = $param['specialId'];
        $url = $param['specialImageUrl'];
        $saveResult = $hotelBookLogic->saveSpecial($hotelId, $specialId, $url, null);
        if (empty($saveResult)) {
            return;
        }   

        $data = array(
            'specialImageUrl' => $url,
        );
        returnJson(200, 200,$data);
    }
    /**
     * 添加个性推荐信息
     * @modify wenhui 2016-10-28 
     * 
     */
    public function addSpecialInfo() {
        if (!$this->hasRight) {
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'specialImageUrl' => 'notnull,notempty,nameLength255',   
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validatePass) {
            return;
        }
        $hotelId = $param['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经通过
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403,409);
        }

        $url = $param['specialImageUrl'];
        // 添加个性推荐信息 返回插入数据的ID
        $specialData = $hotelBookLogic->addSpecialInfo($hotelId, $url);
        if (!$specialData) {
            returnJson(500,508);
        }
        
        returnJson(200,200,$specialData);
    }
    /**
     * 删除个性推荐信息
     * @modify wenhui 2016-10-28
     */
    public function deleteSpecialInfo() {
        if (!$this->hasRight) {
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'specialId' => 'notnull,notempty,id',   
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validatePass) {
            return;
        }

        $hotelId = $param['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经通过
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403,409);
        }

        $specialId = $param['specialId'];
        // 如果此推荐信息已经设置为掌柜说  则不能删除
        $hotelSpecialDetail = $hotelBookLogic->getOneSpecialInfo($specialId);
        if ($hotelSpecialDetail['isactive'] == ENABLE) {          
            return false;
        }
        // 删除个性推荐信息 
        $deleteResult = $hotelBookLogic->deleteSpecialInfos($specialId);
        if (!$deleteResult) {
            return false;
        }

        returnJson(200,200);
    }
    /**
     * 微预定设置 设置掌柜说
     * @modify wenhui 2016-10-28
     */
    public function saveManage() {
        if (!$this->hasRight) {
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'specialId' => 'notnull,notempty,id',
            'index' => 'notnull,notempty',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validatePass) {
            return;
        }

        $hotelId = $param['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经通过
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403,409);
        }

        $specialId = $param['specialId'];
        $index = $param['index'];
        // 设置掌柜说 
        $saveResult = $hotelBookLogic->saveManageInfo($hotelId, $specialId, $index);
        if (!$saveResult) {
            return;
        }

        returnJson(200,200);
    }

    /**
     * 掌柜说排序
     * @modify wenhui 2017-03-09    创建
     */
    public function manageSortIndex() {
        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'indexs' => 'notnull,notempty',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $validateRule = array(
            'specialId' => 'notnull,notempty,id',
            'index' => 'notnull,notempty',
        );
        foreach ($param['indexs'] as $key => $sortIndexs) {
            $this->ajaxValidateReturn($this->validateUtil, $sortIndexs, $validateRule);
        }
        unset($sortIndexs);
 
        $hotel = $param['hotel'];
        $indexs = $param['indexs'];

        $indexValues = array_column($indexs,'index');

        // 校验索引值是否重复
        $indexValues = array_column($indexs, 'index');
        if (count($indexValues) != count(array_unique($indexValues))) {
            returnJson(403, 212);
        }

        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        try {
            $saveResult = $hotelBookLogic->manageSortIndex($hotel, $indexs);
        } catch (\Exception $e) {
            $error = explode(',', $e->getMessage());
            returnJson($error[0], $error[1]);
        }

        // 返回排序后的数据
         // 获取hotelspecial表中的数据 组装新的数据格式
        $hotelSpecialDetail = $hotelBookLogic->getSpecialInfo($hotel);
        $hotelDetail['specialInfo'] = array();
        foreach ($hotelSpecialDetail as &$hotelSpecialItems) {
            // 处理数据
            unset($hotelSpecialItems['hotelentity_id']);
            $hotelSpecialItem = array(
                'specialId' => $hotelSpecialItems['id'],
                'special' => $hotelSpecialItems['special'],
                'specialImageUrl' => $hotelSpecialItems['specialimageurl'],
                'isactive' => $hotelSpecialItems['isactive'],
                'index' => $hotelSpecialItems['sortindex'],
            );
            array_push($hotelDetail['specialInfo'], $hotelSpecialItem);
        }
        unset($hotelSpecialItems);
        // 按‘index’ 升序
        $hotelDetail = my_sort($hotelDetail['specialInfo'], 'index', SORT_ASC, SORT_NUMERIC);

        returnJson(200, 200, $hotelDetail);
    }

    public function applyAuth()
    {
        if (!$this->hasRight) {
            return;
        }
        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',  
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validatePass) {
            return;
        }
        $hotelId = $param['hotel'];
        
        $hotelDetailModel = D('HotelDetail');
        $hotelDetail = $hotelDetailModel->getInfoByHotel($hotelId);
        if(empty($hotelDetail)){
            returnJson(403,409);
        }
        
        $hotelAuthModel = D('HotelAuth');
        $hotelAuthInfo = $hotelAuthModel->getInfo($hotelId);
        if(empty($hotelAuthInfo)){
            $data = array(
                'hotelentity_id' => $hotelId,
                'status' => 1,
                'hotelname' => $hotelDetail['name'],
                'contact' => $hotelDetail['contact'],
                'telephone' => $hotelDetail['mobile'],
                'address' => $hotelDetail['address']
            );
            $hotelAuthModel->add($data);
        }else {
            $where = array(
                'hotelentity_id' => $hotelId
            );
            $data = array(
                'status' => 1,
                'hotelname' => $hotelDetail['name'],
                'contact' => $hotelDetail['contact'],
                'telephone' => $hotelDetail['mobile'],
                'address' => $hotelDetail['address']
            );
            $hotelAuthModel->where($where)->save($data);
        }
        returnJson(200,200);        
    }
    public function cancelAuth()
    {
        if (!$this->hasRight) {
            return;
        }
        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',   
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validatePass) {
            return;
        }
        $hotelId = $param['hotel'];
        $hotelAuthModel = D('HotelAuth');
        $hotelAuthInfo = $hotelAuthModel->getInfo($hotelId);
        if(!empty($hotelAuthInfo) && $hotelAuthInfo['status']=='1'){
            $where = array(
                'hotelentity_id' => $hotelId
            );
            $data = array(
                'status' => 0
            );
            $hotelAuthModel->where($where)->save($data);
        }
        returnJson(200,200);  
    }

    /**
     * 设置优惠券
     * @modify wenhui 2017-01-02    创建
     */
    public function setCoupon() {
        if (!$this->hasRight) {
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'name' => 'notnull,notempty,nameLength50',
            'useMan' => 'notnull,notempty,id',
            'roomType' => 'notnull,notempty,id',
            'couponType' => 'notnull,notempty,id',
            'price' => 'notnull,notempty,decimal8_2',
            'minusPrice' => 'notnull,notempty,decimal8_2',
            'discount' => 'notnull,notempty,integer',
            'number' => 'notnull,notempty,integer',
            'useNumber' => 'notnull,notempty,integer',
            'start' => 'notnull,notempty,isDate',
            'end' => 'notnull,notempty,isDate',
            'roomTypeIds' => 'notnull,notempty',
        );

        if (isset($param['week']) && !empty($param['week'])) {
            $validateRule['week'] = 'notnull,notempty';
        }

        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        if (isset($param['disableDates']) && !empty($param['disableDates'])) {
            foreach ($param['disableDates'] as $date) {
                $validatePass = $this->ajaxValidateReturn($this->validateUtil, $date, 'notnull,notempty,isDate');
                if (empty($validatePass)) {
                    return;
                }
            }
        }

        $hotelId = $param['hotel'];
        $name = $param['name'];
        $couponType = $param['couponType'];
        $price = $param['price'];
        $discount = $param['discount'];
        $number = $param['number'];
        $useNumber = $param['useNumber'];
        $startDate = $param['start'];
        $endDate = $param['end'];
        $week = isset($param['week']) && !empty($param['week']) ? $param['week'] : '';
        $useMan = $param['useMan'];
        $roomType = $param['roomType'];
        $minusPrice = $param['minusPrice'];
        $roomTypeIds = $param['roomTypeIds'];
        $disableDates = isset($param['disableDates']) ? $param['disableDates'] : array();

        // 优惠券领取数量不能大于优惠券数量
        if ($number != 0 && $useNumber > $number) {
            returnJson(403, 1002);
            return;
        }

        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 410);
            return;
        }

        $addResult = $hotelBookLogic->addCoupon($hotelId, $name, $couponType, $price, $discount, $number, $startDate, $endDate, $week, $roomTypeIds, $disableDates, $useMan, $minusPrice, $useNumber, $roomType);
        if ($addResult == false) {
            returnJson(500, 508);
            return;
        }
        returnJson(200, 200);
    }

    /**
     * 修改优惠券
     * @modify wenhui 2017-01-02    创建
     */
    public function editCoupon() {
        if (!$this->hasRight) {
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'couponId' => 'notnull,notempty,id',
            'roomType' => 'notnull,notempty,id',
            'name' => 'notnull,notempty,nameLength50',
            'useMan' => 'notnull,notempty,id',
            'start' => 'notempty,notempty,isDate',
            'end' => 'notempty,notempty,isDate',
            'roomTypeIds' => 'notempty,notempty',
            'useNumber' => 'notnull,notempty,integer',
        );

        if (isset($param['week']) && !empty($param['week'])) {
            $validateRule['week'] = 'notnull,notempty';
        }

        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        if (isset($param['disableDates'])) {
            foreach ($param['disableDates'] as $date) {
                $validatePass = $this->ajaxValidateReturn($this->validateUtil, $date, 'notempty,notempty,isDate');
                if (empty($validatePass)) {
                    return;
                }
            }
        }

        $hotelId = $param['hotel'];
        $couponId = $param['couponId'];
        $name = $param['name'];
        $startDate = $param['start'];
        $endDate = $param['end'];
        $week = isset($param['week']) && !empty($param['week']) ? $param['week'] : '';
        $useMan = $param['useMan'];
        $roomType = $param['roomType'];
        $roomTypeIds = $param['roomTypeIds'];
        $disableDates = isset($param['disableDates']) ? $param['disableDates'] : array();
        $useNumber = $param['useNumber'];

        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 410);
            return;
        }

        $addResult = $hotelBookLogic->editCoupon($couponId, $name, $startDate, $endDate, $week, $roomTypeIds, $disableDates, $useMan, $useNumber, $roomType);
        if ($addResult == false) {
            returnJson(500, 508);
            return;
        }
        returnJson(200, 200);
    }

    /**
     * 删除优惠券
     * @modify wenhui 2017-01-02    创建
     */
    public function deleteCoupon() {
        if (!$this->hasRight) {
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'couponId' => 'notnull,notempty,id',
        );

        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        $hotelId = $param['hotel'];
        $couponId = $param['couponId'];

        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 410);
            return;
        }

        $result = $hotelBookLogic->deleteCoupon($couponId);
        if ($result == false) {
            returnJson(500, 510);
            return;
        }

        returnJson(200, 200);
    }

    /**
     * 获取优惠券
     * @modify wenhui 2017-01-02    创建
     */
    public function hasCoupon() {
        if (!$this->hasRight) {
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
        );

        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        $hotelId = $param['hotel'];

        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 410);
            return;
        }

        $data = $hotelBookLogic->getCoupon($hotelId);

        returnJson(200, 200, $data);
    }

    /**
     * 获取优惠券详情
     * @modify wenhui 2017-01-02    创建
     */
    public function hasCouponDetail() {
        if (!$this->hasRight) {
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'couponId' => 'notnull,notempty,id',
        );

        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        $hotelId = $param['hotel'];
        $couponId = $param['couponId'];

        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 410);
            return;
        }

        $data = $hotelBookLogic->getCoupon($hotelId, $couponId);

        // 处理数据
        $dateItem = array();
        foreach ($data[0]['couponDate'] as $key => $date) {
            $dateItem[] = $date['date'];
        }
        unset($date);

        $data[0]['couponDate'] = $dateItem;
        $data[0]['couponStart'] = date('Y-m-d',strtotime($data[0]['couponStart']));
        $data[0]['couponEnd'] = date('Y-m-d',strtotime($data[0]['couponEnd']));

        returnJson(200, 200, $data[0]);
    }

    /**
     * 获取优惠券领取记录
     * @modify wenhui 2017-01-02    创建
     */
    public function getHotelUserCoupons() {
        if (!$this->hasRight) {
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'couponId' => 'notnull,notempty,id',
        );

        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }

        $hotelId = $param['hotel'];
        $couponId = $param['couponId'];

        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 410);
            return;
        }

        $data = $hotelBookLogic->getHotelUserCoupon($hotelId, $couponId);

        returnJson(200, 200, $data);
    }

    public function toggleBookShareKey()
    {
        if (!$this->hasRight) {
            return;
        }
        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'status' => 'notnull,notempty,booleanInt',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validatePass) {
            return;
        }
    
        // 校验客栈是否启用微预定
        $hotelId = $param['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }
    
        // 修改信息
        $hotelDetailModel = D('HotelDetail');
        $toggleResult = $hotelDetailModel->saveShareStatus($hotelId, $param['status']);
        if (empty($toggleResult)) {
            returnJson(500, 506);
            return;
        }
    
        returnJson(200, 200);
    }
}
